<?php

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\Config;
use think\Log;
/**
 * 小程序im接口
 */
class Ims extends Api
{
    protected $noNeedLogin = ['chatlog','sensitive_checking','pushim'];
    protected $noNeedRight = ['*'];

    /**
     *
     *
     */
    public function chatlog()
    {

        $userid = $this->request->post('userid','');
        $zxsid = $this->request->post('zxsid','');
        if (!$userid || ! $zxsid) {
            $this->error(__('参数有误'));
        }
        $zxs = Db::name('mental_xlzxinfo')->where('id',$zxsid)->where('status',1)->find();
        if(empty($zxs)){
            $this->error('不是咨询师');
        }
        $zxsid = 'zxs'.$zxsid;
        $log = [];
        $log['user_id'] = $userid;
        $log['zxs_id'] = $zxsid;
        $info = Db::name('im_chatlog')->where($log)->whereTime('create_time','today')->select(); //获取今天的数据
        $data['ischat_record'] = 1;
        if(empty($info)){
            Db::startTrans();
            try {
                Log::info('进来发送im消息');
                $arr = [
                  'user_id' => $userid,
                  'zxs_id' => $zxsid,
                  'create_time' => time(),
                ];
                Db::name('im_chatlog')->insert($arr);
                $data['ischat_record'] = 0;
                //咨询师给用户发消息
                \app\common\library\Ims::send_msg($zxsid,$userid);
                Log::info('到这里了');
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->setError($e->getMessage());
            }
        }
        $this->success(__('查询成功'), $data);
    }


    //验证发送的消息是否包含敏感词
    public function sensitive_checking()
    {
        if ($this->request->isPost()) {
            Log::info('进来了');
            $sensitive_text =  config('site.sensitive_text');
            Log::info($sensitive_text);
            $actionCode = $errCode = $nextCode =0;
            $errMsg = $errDlt = '';

            //回调来的消息内容
            $content1 = $this->request->post('content','');;
            Log::info('内容');
            Log::info($content1);
            $a = str_replace('&quot;', '"', $content1);
            $content2 = json_decode($a,true);
            $content = $content2['content'];
            Log::info($content);
            $pattern = '/^1(3|4|5|6|7|8|9)\d{9}$/';
            //如果是手机号
            if (preg_match($pattern, $content)) {
                $actionCode = 0;
                $errCode = 8100;
                $errMsg = '抱歉，消息内容含有敏感词，请调整后再发送~感谢配合。';
                $errDlt = '系统不允许发送手机号';
                $nextCode = 1;
                Log::info('手机号进来了');
            }else{
                foreach (explode(",", $sensitive_text) as $val) {
                    if(!empty($val)){
                        Log::info($val);
                        if (strpos($content, $val) !== false) {
                            $actionCode = 0;
                            $errCode = 8200;
                            $errMsg = '抱歉，消息内容含有敏感词，请调整后再发送~感谢配合。';
                            $errDlt = '系统不允许发送辱骂词';
                            $nextCode = 1;
                        }
                    }
                }
            }
            $data = [
                'actionCode'=>$actionCode,
                'errCode'=>$errCode,
                'errMsg'=>$errMsg,
                'errDlt'=>$errDlt,
                'nextCode'=>$nextCode,
            ];
            Log::info('到这里了');
            return json_encode($data);
            die();

        }
    }

    /**
     * 验证当前用户的im token
     * @return void
     */
    public function token_check(){
        Log::info('token验证进来了');
        if ($this->request->isPost()) {
            $userId = $this->request->post('userId','');
            $role = $this->request->post('role','');
            if(empty($userId) || empty($role)){
                $this->error('缺少参数');
            }
            $data = \app\common\library\Ims::imtoken_check($userId,$role);
            $this->success(__('查询成功'), $data);
        }
        $this->error();
    }

    /**
     * 同步咨询师用户
     * @return void
     */
    public function  pushim()
    {
        $zxs_list = Db::name('mental_xlzxinfo')->where('status',1)->select();
        foreach ($zxs_list as $z) {
            $data = \app\common\library\Ims::pilang_account_check($z['id']);
        }
        die('完成');
    }
}
